package com.lsh.oms.core.service.handler;

import com.alibaba.fastjson.JSON;
import com.lsh.base.common.model.CommonResult;
import com.lsh.oms.api.model.status.CancelOrderDto;
import com.lsh.oms.core.constant.ReturnCode;
import com.lsh.oms.core.enums.OrderStatus;
import com.lsh.oms.core.model.order.OrderHead;
import com.lsh.oms.core.proxy.handler.OrderConcurrentHandler;
import com.lsh.oms.core.service.business.CancelOrderService;
import com.lsh.oms.core.service.order.OrderCancelService;
import com.lsh.oms.core.service.order.OrderQueryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author panxudong
 * @date 17/3/23
 */
@Service
public class CancelOrderHandler {

    private static final Logger logger = LoggerFactory.getLogger(CancelOrderHandler.class);

    @Resource(name = "cancelOrderUserServiceImpl")
    private CancelOrderService cancelOrderUserService;

    @Resource(name = "cancelOrderMisServiceImpl")
    private CancelOrderService cancelOrderMisService;

    @Resource(name = "cancelOrderTempServiceImpl")
    private CancelOrderService cancelOrderTempService;

    @Resource(name = "cancelOrder4misCodeServiceImpl")
    private CancelOrderService cancelOrder4misCodeService;

    @Autowired
    private OrderCancelService orderCancelService;

    @Autowired
    private OrderQueryService orderQueryService;

    public CommonResult execute(CancelOrderDto cancelOrderDto, CancelType cancelType, String logSign, Long venderId) {
        String prefix = "[CancelOrder][" + cancelType + "][" + logSign + "]";

        logger.info(prefix + "[start][Request params:" + JSON.toJSONString(cancelOrderDto) + "]");

        CommonResult commonResult;

        Long orderCode = cancelOrderDto.getOrderCode();
        if (!OrderConcurrentHandler.lockOrder(orderCode)) {
            commonResult = new CommonResult<>(ReturnCode.REQUEST_FAIL, "订单正在处理,请稍后重试...");
            logger.info(prefix + "[end][Response result:" + JSON.toJSONString(commonResult) + "]");
            return commonResult;
        }

        try {
            OrderHead orderHead = orderQueryService.findByCode(orderCode, false, false);
            if (orderHead == null) {
                return new CommonResult(ReturnCode.REQUEST_FAIL, "订单不存在");
            }

            if (orderHead.getOrderStatus() == OrderStatus.ORDER_PRE.getIndex()) {
                return orderCancelService.orderCancel(orderCode, "po-order-cancel-order");
            }

            if (orderHead.getOrderStatus() >= OrderStatus.ORDER_CANCEL_CUSTOMERSERVICE.getIndex()) {
                return this.createResult(ReturnCode.REQUEST_SUCCESS, "取消处理成功");
            }

            if (cancelType == CancelType.MIS) {
                cancelOrderDto.setMisCodeCancelFalg(false);
                commonResult = cancelOrderMisService.cancelOrder4split(cancelOrderDto, logSign, venderId);
            } else if (cancelType == CancelType.USER) {
                cancelOrderDto.setMisCodeCancelFalg(false);
                commonResult = cancelOrderUserService.cancelOrder4split(cancelOrderDto, logSign, venderId);
            } else if (cancelType == CancelType.MIS_CODE) {
                commonResult = cancelOrder4misCodeService.cancelOrder4split(cancelOrderDto, logSign, venderId);
            } else {
                cancelOrderDto.setMisCodeCancelFalg(false);
                commonResult = cancelOrderTempService.cancelOrder4split(cancelOrderDto, logSign, venderId);
            }
            logger.info(prefix + "[end][Response result:" + JSON.toJSONString(commonResult) + "]");
        } finally {
            OrderConcurrentHandler.unlockOrder(orderCode);
        }

        return commonResult;
    }

    private CommonResult<Object> createResult(String code, String message) {
        CommonResult<Object> commonResult = new CommonResult<>(code, message);
        return commonResult;
    }

    public enum CancelType {
        USER, MIS, TEMP, MIS_CODE
    }

}
